<?php
namespace App\Handlers;

/*
 * This file is part of the jcc/laravel-vote.
 *
 * (c) jcc <changejian@gmail.com>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

trait CanBeVoted
{   
    protected $TYPE = [
        'UP_VOTE' => 1,  //点赞
        'FAVOUR'  => 2,   //收藏
        'CARD' => 3,     //打卡
//         'READ' => 4,     //浏览
        'DOWN_VOTE' => 5,    //反对
    ];
    
    /**
     * 是否被点赞
     * Check if user is voted by given user.
     *
     * @param $user
     *
     * @return bool
     */
    public function isUpVotedBy($user)
    {
        return $this->typeVoters($this->TYPE['UP_VOTE'])->get()->contains($user);
    }
    
    /**
     * 是否被收藏
     * Check if user is voted by given user.
     *
     * @param $user
     *
     * @return bool
     */
    public function isFavouredBy($user)
    {
        return $this->typeVoters($this->TYPE['FAVOUR'])->get()->contains($user);
    }
    
    /**
     * 是否被打卡
     * Check if user is voted by given user.
     *
     * @param $user
     *
     * @return bool
     */
    public function isCardedBy($user)
    {
        return $this->typeVoters($this->TYPE['CARD'])->get()->contains($user);
    }
    
    /**
     * Check if user is voted by given user.
     *
     * @param $user
     *
     * @return bool
    */
    public function isVotedBy($user)
    {
        return $this->voters()->get()->contains($user);
    }

    /**
     * Return the total vote score
     *
     * @return int
     */
    //     public function countTotalVotes()
    //     {
    //         $downVote = $this->countVoters($this->TYPE['DOWN_VOTE']);
    //         $upVotes = $this->countVoters($this->TYPE['UP_VOTE']);
    //         return $upVotes - $downVote;
    //     }

    /**
     * Count the number of up votes.
     *
     * @return int
     */
    public function countUpVoters()
    {
        return $this->countVoters($this->TYPE['UP_VOTE']);
    }

    /**
     * Count the number of card users.
     *
     * @return int
     */
    public function countCardUsers()
    {
        return $this->countVoters($this->TYPE['CARD']);
    }

    /**
     * Count the number of read users.
     *
     * @return int
     */
//     public function countReadUsers()
//     {
//         return $this->countVoters($this->TYPE['READ']);
//     }

    /**
     * Count the number of favour users.
     *
     * @return int
     */
    public function countFavourUsers()
    {
        return $this->countVoters($this->TYPE['FAVOUR']);
    }

    /**
     * Count the number of down votes.
     *
     * @return int
     */
    public function countDownVoters()
    {
        return $this->countVoters($this->TYPE['DOWN_VOTE']);
    }

    /**
     * Count the number of voters of the given type.
     *
     * @param  int $type
     *
     * @return int
     */
    public function countVoters($type = 1)
    {
        $voters = $this->voters();

        if(!is_null($type)) $voters->wherePivot('type', $type);     

        return $voters->count();
    }
    
    /**
     * Return voters of the given type
     * 
     * @param  int $type
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function typeVoters($type = 1) {
        $voters = $this->voters()->wherePivot('type', $type);
        return $voters;
    }
    
    /**
     * Return voters of the up_vote type
     *
     * @param  int $type
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function upVoters() {
        $voters = $this->voters()->wherePivot('type', $this->TYPE['UP_VOTE']);
        return $voters;
    }
    
    /**
     * Return voters of the favour type
     *
     * @param  int $type
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function favourUsers() {
        $voters = $this->voters()->wherePivot('type', $this->TYPE['FAVOUR']);
        return $voters;
    }
    
    /**
     * Return voters of the card type
     *
     * @param  int $type
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function cardUsers() {
        $voters = $this->voters()->wherePivot('type', $this->TYPE['CARD']);
        return $voters;
    }
    
    /**
     * Return voters of the read type
     *
     * @param  int $type
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
//     public function readUsers() {
//         $voters = $this->voters()->wherePivot('type', $this->TYPE['READ']);
//         return $voters;
//     }

    /**
     * Return voters.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function voters()
    {
        $property = property_exists($this, 'vote') ? $this->vote : __CLASS__;

        return $this->morphToMany($property, 'votable', $this->vote_table ?: 'votes', '');
    }
}